package com.boss.salary.service.impl;

import com.boss.salary.dao.IAgencyInfoDao;
import com.boss.salary.dao.IPersonOperateDao;
import com.boss.salary.entity.AgencyVerify;
import com.boss.salary.service.IRedisService;
import com.boss.salary.service.ISalaryHistoryAsyncChildrenService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

@Service
@EnableAsync
public class SalaryHistoryAsyncChildrenServiceImpl implements ISalaryHistoryAsyncChildrenService {
    /**
     * 引入日志框架常量
     */
    @Lazy
    private static final Logger logger = LoggerFactory.getLogger(SalaryHistoryAsyncChildrenServiceImpl.class);
    private static DateFormat df = new SimpleDateFormat("yyyy-MM");
    @Autowired
    private IRedisService redisService;
    @Autowired
    private IPersonOperateDao personOperateDao;
    @Autowired
    private IAgencyInfoDao agencyInfoDao;

    @Autowired
    private SalaryHistoryServiceImpl salaryHistoryService;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public  void saveMonthEnds(List<AgencyVerify> agencyVerifyEndList, Integer year, String province, String agencyId, Map<String, String> monthMap, Map<String, String> agencyNameMap, Map<String, String> agencyCodeMap, List<Object[]> personOperateUpdateList, StringBuffer requestMessage)throws Exception {

//        try{
            //Thread.currentThread().sleep(300);
            // 批量修改单位审核表的上报状态后，增加下个月的初始数据
            logger.info("==================批量修改审核表数据 Begin ================");
            salaryHistoryService.batchAgencyVerify(year, province,agencyId, monthMap,agencyNameMap,agencyCodeMap);
            logger.info("==================批量修改审核表数据 End ================");

            logger.info("==================批量存储单位历史表数据 Begin ================");
            // 将单位信息存储到历史表
            salaryHistoryService.batchSaveAgencyHistory(province,agencyId, monthMap);
            logger.info("==================批量存储单位历史表数据 End ================");
            logger.info("==================批量存储人员历史表数据 Begin ================");
            // 将人员信息存储到历史表
            salaryHistoryService.batchSavePersonHistory(province,agencyId, monthMap);
            logger.info("==================批量存储人员历史表数据 End ================");
            logger.info("==================批量存储工资表历史表数据 Begin ================");
            // 转换工资表数据并添加到历史表
            salaryHistoryService.batchSaveSalaryHistory(province,agencyId, monthMap,0);
            logger.info("==================批量存储工资表历史表数据 End ================");
            logger.info("==================批量存储工资表月初表数据 Begin ================");
            // 转换工资表数据并添加到历史表
            salaryHistoryService.batchSaveSalaryMonthBegin(province, agencyId,agencyVerifyEndList);
            logger.info("==================批量存储工资表月初表数据 End ================");
            logger.info("==================批量存储工资表月初表数据 Begin ================");
            //支付回盘信息保存到历史表中，同时清空回盘信息
            salaryHistoryService.batchPaymentBack(agencyId,monthMap);
            logger.info("==================批量存储支付回盘数据 End ================");
            logger.info("==================批量存储银行回盘数据 Begin ================");
            //银行回盘信息保存到历史表中，同时清空回盘信息
            salaryHistoryService.batchBankBack(agencyId,monthMap);
            logger.info("==================批量存储银行回盘数据 End ================");
            //若自动上报单位不为空则修改上月编报的数据为本月
            if(!CollectionUtils.isEmpty(personOperateUpdateList)){
                //将personOperateUpdateList才分 如果id相等走方法
                for(Object[] personOperate:personOperateUpdateList){
                    for(int i=0;i<personOperate.length;i++ ){
                        if(personOperate[i].equals(agencyId)){
                            // 批量修改自动上报单位上月修改数据状态改为下个月
                            logger.info("==================批量修改自动上报单位上月修改数据状态改为下个月 Begin ================");
                            personOperateDao.batchUpdateAutoMonthEnd(personOperateUpdateList);
                            logger.info("==================批量修改自动上报单位上月修改数据状态改为下个月 End ================");
                            logger.info("==================批量修改自动上报单位上月附件数据状态改为下个月 Begin ================");
                            personOperateDao.batchUpdateAutoFile(personOperateUpdateList);
                            logger.info("==================批量修改自动上报单位上月附件数据状态改为下个月 End ================");
                        }
                    }
                }
            }
//        }catch (Exception e){
//            logger.info("月结后续方法错误：",e);

//        }

    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public  void saveMonthEndsDouble(List<AgencyVerify>agencyVerifyDoubleList,Integer year, String province, String agencyId, Map<String, String> monthMap, Map<String, String> agencyNameMap, Map<String, String> agencyCodeMap, List<Object[]> personOperateUpdateList, StringBuffer requestMessage) throws Exception{

        int isThirteen=0;
        for(AgencyVerify agencyVerify:agencyVerifyDoubleList){
            if(agencyVerify.getAgencyId().equals(agencyId)){
                isThirteen=agencyVerify.getIsThirteen();
            }
        }
        logger.info("==================批量修批次改审核表数据 Begin ================");
        salaryHistoryService.updatebatchAgencyVerify(agencyId,agencyVerifyDoubleList);
        logger.info("==================批量修改批次审核表数据 End ================");
        // 转换工资表数据并添加到历史表
        logger.info("==================批量存储批次工资表历史表数据 End ================");
        salaryHistoryService.batchSaveSalaryHistory(province,agencyId, monthMap,isThirteen);
        logger.info("==================批量存储批次工资表月初表数据 Begin ================");
        // 转换工资表数据并添加到历史表
        logger.info("==================批量存储工资表月初表数据 End ================");
        salaryHistoryService.batchDeletedSalaryMonthBegin(province, agencyId,isThirteen);
        logger.info("==================批量存储工资表月初表数据 Begin ================");
        //支付回盘信息保存到历史表中，同时清空回盘信息
        logger.info("==================批量批次存储支付回盘数据 End ================");
        salaryHistoryService.batchPaymentBack(agencyId,monthMap);
        logger.info("==================批量批次存储银行回盘数据 Begin ================");
        //银行回盘信息保存到历史表中，同时清空回盘信息
        logger.info("==================批量存储批次银行回盘数据 End ================");
        salaryHistoryService.batchBankBack(agencyId,monthMap);
        logger.info("==================批量存储批次银行回盘数据 Begin ================");
        //情况批次表
        logger.info("==================批量删除批次表数据数据 End ================");
        salaryHistoryService.deletedDouble(agencyId,monthMap);
        logger.info("==================批量删除批次表数据数据 Begin ================");
    }
}
